﻿
Partial Class WebForm_ManageCatagory_SearchCatagory
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.Page.IsPostBack Then
            Me.BindDDLDivision()
            Me.BindDDLCatagoryLevel()
            Me.BindGV()
        End If
    End Sub

    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
    End Sub

    Private Sub BindDDLCatagoryLevel()
        Dim ddlControl As DropDownList = Me.ddlCatagoryLevel
        ddlControl.Items.Clear()
        ddlControl.Items.Add(New ListItem("Second", 2))
        ddlControl.Items.Add(New ListItem("Third", 3))
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("Select top 1 * from CodeCatagoryHeader ")
        sql.AppendFormat("where NewDescription = '{0}' ", Me.txtNewDescription.Text.Trim().Replace("'", "''"))
        sql.AppendFormat("and Division = '{0}' ", Me.ddlDivision.SelectedValue)

        If dat.GetData(sql).Rows.Count > 0 Then
            MyObj.ShowMessage("The Description already exists.")
        Else
            Using scope As New Transactions.TransactionScope
                sql.Length = 0
                sql.AppendFormat("Insert into CodeCatagoryHeader(Division,LevelName,NewDescription,CatagoryLevel) ")
                sql.AppendFormat("select '{0}' ", Me.ddlDivision.SelectedValue)
                sql.AppendFormat(",'Signature' ")
                sql.AppendFormat(",'{0}' ", Me.txtNewDescription.Text.Trim().Replace("'", "''"))
                sql.AppendFormat(",'{0}' ", Me.CatagoryLevel)
                dat.AccData(sql)
                scope.Complete()
            End Using
            Me.txtNewDescription.Text = ""
            Me.BindGV()
        End If
    End Sub

    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub ddlCatagoryLevel_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCatagoryLevel.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Dim key As DataKey = gv.DataKeys(e.RowIndex)
        Dim txtNewDescriptionEdit As TextBox = TryCast(gv.Rows(e.RowIndex).FindControl("txtNewDescriptionEdit"), TextBox)
        sql.Length = 0
        sql.AppendFormat("Select top 1 * from CodeCatagoryHeader ")
        sql.AppendFormat("where NewDescription = '{0}' ", txtNewDescriptionEdit.Text.Trim().Replace("'", "''"))
        sql.AppendFormat("and Division = '{0}' ", key("Division"))
        sql.AppendFormat("and HeaderID <> '{0}' ", key("HeaderID"))

        If dat.GetData(sql).Rows.Count > 0 Then
            MyObj.ShowMessage("The Description already exists.")
        Else
            Using scope As New Transactions.TransactionScope
                sql.Length = 0
                sql.AppendFormat("Update CodeCatagoryHeader ")
                sql.AppendFormat("set NewDescription = '{0}' ", txtNewDescriptionEdit.Text.Trim().Replace("'", "''"))
                sql.AppendFormat("where HeaderID = '{0}' ", key("HeaderID"))
                dat.AccData(sql)

                scope.Complete()
            End Using
            gv.EditIndex = -1
            Me.BindGV()
        End If
    End Sub

    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        Me.gv.EditIndex = e.NewEditIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv.RowCancelingEdit
        Me.gv.EditIndex = -1
        Me.BindGV()
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Dim key As DataKey = gv.DataKeys(e.RowIndex)

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("delete from CodeCatagoryHeader ")
            sql.AppendFormat("where HeaderID = '{0}' ", key("HeaderID"))
            sql.AppendFormat(";delete from CodeCatagoryItem ")
            sql.AppendFormat("where HeaderID = '{0}' ", key("HeaderID"))
            dat.AccData(sql)
            scope.Complete()
        End Using
        Me.BindGV()
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select * from CodeCatagoryHeader ")
        sql.AppendFormat(" Where 1=1 ")
        sql.AppendFormat(" And CatagoryLevel='{0}' ", Me.CatagoryLevel)
        If Me.ddlDivision.SelectedValue <> "" Then
            sql.AppendFormat(" And Division = @Division ")
            sql.AddWithValue("Division", Me.ddlDivision.SelectedValue)
        End If

        sql.AppendFormat(" order by NewDescription ")

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

    End Sub

    Public ReadOnly Property CatagoryLevel() As String
        Get
            Return Me.ddlCatagoryLevel.SelectedValue
        End Get
    End Property
End Class
